﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Net;
using ZyGames.Framework.Common.Serialization;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 申请使用延时药水
	/// </summary>
	public class Action20050 : BaseAction
	{
		private int tableId;
		private int userId;
		private GameUser _currentGameUser;
		private TableData _currentTable;

		private bool result = false;
		private string log = "";
		public Action20050(HttpGet actionGetter)
			: base((short)ActionType.DZApplyUseDelayMedicine, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableID", ref tableId) &&
				httpGet.GetInt("UserID", ref userId))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			_currentTable = GameRoom.Current.GetTableData(tableId);
			if (_currentTable == null)
			{
				Tool.WriteWarmingLine("消息20050获得房间不存在");
				return false;
			}

			_currentGameUser = GameTable.Current.GetUser(userId);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息20050玩家不存在");
				return false;
			}
			if(_currentTable.CurrentBetPos == null)
			{
				Tool.WriteErrorLine("消息20050 当前玩家不再操作中，不可以使用延时药水");
				return false;
			}
			//TODO:判断是否是当前玩家正在操作,如果不是的话则退出
			if (_currentTable.CurrentBetPos.UserId == userId)
			{
				//检测玩家金币是够足够

				var time = _currentTable.OperationSecTimeout + _currentTable.OperationSecTimeoutOffset + _currentTable.MedecineTimeDelay;
				//假设足够,时间重置
				_currentTable.ReStartTimer(time);

				var parameters = new Parameters();
				parameters["TableID"] = tableId;
				parameters["UserID"] = userId;
				parameters["Time"] = time;
				GameTable.Current.SyncNotifyAction((int)ActionType.DZBrocastUseDelayMedicine, _currentTable, parameters, t =>
				{
					Console.WriteLine("Action 20051 广播玩家试用延时药水");
				});
			}
			else
			{
				log = "当前没在你的下注回合，您不可以申请试用延时药水";
			}
			result = true;
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this.result);
			this.PushIntoStack(this.log);
		}
	}
}
